package lab03.test03;


//3、	在某目录下创建一个文本文件test.txt，通过字节输出流向文件中写入“Beijing欢迎你！”，
// 通过字节输入流读出文件中的内容并显示；如果在已有文件中再追加“我的学号是：
// 你自己的学号”如何实现？
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test03 {
    public static void main(String[] args) {
        String path = "D:\\dev\\shiyan\\test.txt";
        FileOutputStream fileOutputStream = null;
        FileInputStream fileInputStream = null;


        try{
            String orgin ="BeiJing 欢迎你!";
            String add = "我的学号是：1606101086";
            fileOutputStream = new FileOutputStream(path);
            fileInputStream = new FileInputStream(path);
            fileOutputStream.write(orgin.getBytes());//字符串转化字节
            int data = 1;
            while (data != -1){
                data = fileInputStream.read();
                System.out.print((char)data);
            }//读取乱码，未解决
            int addint = 1;
            while (addint !=-1){
                addint =fileInputStream.read();
                fileOutputStream.write(add.getBytes());
            }//追加

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
